package com.ecity.view {
	import com.ecity.events.GameEvent;
	import com.ecity.events.GameState;
	import com.ecity.table.Desktop;
	import com.ecity.table.HitButton;
	import com.ecity.table.Player;
	import com.ecity.table.TableDisplayObject;
	import com.ecity.util.GameLoader;
	import com.ecity.util.HashMap;
	import flash.events.NativeDragEvent;
	
	/**
	 * ...
	 * @author ss
	 */
	public class Viewer extends TableDisplayObject implements IViewer {
		private var _button:HitButton;
		private var _desktop:Desktop;
		private var _resReady:Boolean = false;//资源是否加载完毕； 
		private var _isStarted:Boolean = false;
		private var _gameTurn:int = 0;
		private var _playerMap:HashMap = new HashMap();
		
		public function Viewer(){
			initResource();
			_button = new HitButton();
			_desktop = new Desktop();
			this.addChildAtTop(_button);
			this.addChildAtBottom(_desktop);
		}
		
		/**
		 * 加载游戏资源
		 */
		private function initResource():void {
			var assetLoder:GameLoader = new GameLoader();
			assetLoder.startLoad(onSourceHandler);
		}
		/**
		 * 加载资源情况下各种判断
		 */
		private function onSourceHandler():void {
			if (arguments[0] == GameLoader.INIT_UI) {
				_desktop.initChildren("desktop");
			}else if (arguments[0] == GameLoader.PROGRESS) {
				
			}else {
				_resReady = true;
				_desktop.addEventListener(GameEvent.BACKGROUND_INITED,requestHandler);
				_button.initChildren("button");
			}
		}
		
		/**
		 *（I） 从开始页面进入游戏页面，这一步能够做到让游戏开始
		 */
		public function startGame():void{
			_isStarted = true;
			_desktop.showBackground();
		}
		
		/**
		 *（I） 判断相应的按钮是否碰撞到系统发送来的坐标方块
		 * 
		 * @param	hx
		 * @param	hy
		 */
		public function hitTestCase(hx:int, hy:int):void {
			if(GameState.hitable){
				switch(GameState.state) {
					case GameState.GAME_READY:
						if (_button.hitTest(x, y, "b_startGame")) {
							if (_gameTurn == 1) {
								initGame();
							}else {
								resetGame();
							}
						}
						break;
				}
			}
		}
		
		//=======================================================
		private function requestHandler(e:GameEvent):void {
			switch(e.type) {
				case GameEvent.BACKGROUND_INITED:
					setGameState("b_startGame",GameState.GAME_READY);
					break;
			}
		}
		
		private function setGameState(btnKey:String,state:String):void {
			_button.addButton("b_startGame");
			GameState.state = state;
			GameState.hitable = true;
		}
		//=======================================================
		/**
		 * 初始化游戏
		 */
		private function initGame():void {
			initPlayer();
		}
		/**
		 * 重置游戏
		 */
		private function resetGame():void {
			//initPlayer();
		}
		
		/**
		 * 玩游戏
		 */
		private function playGame():void {
			//initPlayer();
		}
		
		private function initPlayer():void {
			for (var i:int = 0; i < 4; i++) {
				var p:Player = new Player(i);
				_playerMap.put("p" + i, p);
				this.addChildAs(p);
			}
		}
		
		private function setPlayer(func:Function):void {
			_playerMap.eachValue(func);
		}
		
		
		
		
	}
}